gpt4 book ai didi

java - 如何以编程方式更改图像的不透明度并将其保存在图库中

转载 作者:太空宇宙 更新时间:2023-11-04 11:07:28 25 4
gpt4 key购买 nike

我有两个图像彼此重叠,通过在运行时更改顶部图像的不透明度,然后组合这两个图像(意味着使这两个图像成为一个图像)并将其保存在图库中。在android studio中如何实现这一点?

最佳答案

如果图像是全屏的,只需制作您自己的屏幕截图即可。如果您愿意,有很多很棒的库可以为您执行此操作,或者您可以使用我放入名为 ImageHelper 的类中的简单代码,如下所示:

public static Bitmap takeScreenshotOfView(Activity context, Bitmap.CompressFormat compressFormat){
Bitmap screenshot = null;

try {
// create bitmap screen capture
View v1 = context.getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
screenshot = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

File imageFile = new File(context.getFilesDir() + File.separator + "A35_temp" + File.separator + "screenshot_temp");

FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;

screenshot.compress(compressFormat, quality, outputStream);
outputStream.flush();
outputStream.close();

} catch (Throwable e) {
// Several error may come out with file handling or OOM
e.printStackTrace();
}

return screenshot;
}

然后像这样使用它:

Bitmap screenshot = ImageHelper.takeScreenshotOfView(this, Bitmap.CompressFormat.JPEG);

现在,如果您只需要捕获 ImageView 区域,您可以通过 View 获取它,也可以获取两个 ImageView 的父级,以便将它们包含在内。您还可以指定全屏,但从 imageView 起点和终点开始。这是我为管理各个区域的屏幕截图而编写的一些示例代码。

 private void convertCardToBitmap(boolean sendOnComplete){
if(!mIsForStore) {
Toast.makeText(this, getString(R.string.downloading_to_gallery), Toast.LENGTH_LONG).show();

}

CardView cardView = (CardView)findViewById(R.id.my_image_view_id);

cardView.setDrawingCacheEnabled(true);

cardView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
//cardView.measure((int)height, (int)width);
cardView.layout(0, 0, cardView.getMeasuredWidth(), cardView.getMeasuredHeight());
//cardView.layout(0, 0, (int)width, (int)height);
cardView.buildDrawingCache(true);

Bitmap bitmap = Bitmap.createBitmap(cardView.getDrawingCache());

saveBitmapToGallery(bitmap, sendOnComplete);

}
private void saveBitmapToGallery(Bitmap cardToSave, boolean sendFileWhenSaved){
OutputStream output;
// Find the SD Card path
File filepath = Environment.getExternalStorageDirectory();

// Create a new folder in SD Card
File dir = new File(filepath.getAbsolutePath() + "/MyAppPathDir/");
dir.mkdirs();

String imageName = mSelectedPerson.getFirstName() + "_" + mSelectedPerson.getLastName() + "_" + FileNameHelper.getNowTimeStamp();

// Create a name for the saved image
File file = new File(dir, imageName + ".jpg");

try {
int count = 1;
//if file exists add 1 on the end and loop until finding a name that doesn't exist.
while(file.exists()){
file = new File(dir, imageName + count + ".jpg");
count++;

}

output = new FileOutputStream(file);

// Compress into png format image from 0% - 100%
cardToSave.compress(Bitmap.CompressFormat.JPEG, 100, output);
output.flush();
output.close();

addImageToGallery(file.getAbsolutePath());

if(sendFileWhenSaved){
Intent intent = getIntent();
intent.putExtra(Globals.INTENT_KEYS.KEY_FILE_TO_SHARE, file.getAbsolutePath());
intent.putExtra(Globals.INTENT_KEYS.KEY_SELECTED_IMAGE, mSelectedCardModel);
setResult(RESULT_OK, intent);
finish();

} else if (mIsForStore) {
uploadImageToS3(file.getAbsolutePath());

} else{
Toast.makeText(this, getString(R.string.saved_to_gallery), Toast.LENGTH_LONG).show();
finish();

}

} catch (Exception e) {
Toast.makeText(this, getString(R.string.error_failed_save_to_gallery) + e.getMessage(), Toast.LENGTH_LONG).show();

}
}
private void uploadImageToS3(String filePath){
final File newImageFile = new File(filePath);
showProgressDialog(TAG, getString(R.string.loading_please_wait));

//For auth route
BasicAWSCredentials credentials = new BasicAWSCredentials(CognitoManager.getS3ClientID(), CognitoManager.getS3ClientSecret());

AmazonS3Client s3 = new AmazonS3Client(credentials);
TransferUtility transferUtility = new TransferUtility(s3, this);
TransferObserver observer = transferUtility.upload(CognitoManager.getS3BucketName(), newImageFile.getName(), newImageFile);
observer.setTransferListener(new TransferListener() {
@Override
public void onStateChanged(int id, TransferState state) {
if(state.compareTo(TransferState.COMPLETED) == 0){
String imgURLOfUploadComplete = "https://s3.amazonaws.com/" + CognitoManager.getS3BucketName() + "/" + newImageFile.getName();
hideProgressDialog(TAG);
Intent intent = new Intent();
intent.putExtra(Globals.INTENT_KEYS.KEY_IMAGE_URL, imgURLOfUploadComplete);
setResult(Activity.RESULT_OK, intent);
if(newImageFile.exists()){
newImageFile.delete();

}
finish();

}

}
@Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
if(bytesTotal != 0) {
//For viewing progress
int percentage = (int) (bytesCurrent / bytesTotal * 100);
}
}
@Override
public void onError(int id, Exception ex) {
A35Log.e(TAG, getString(R.string.error_uploading_s3_part1) + id + getString(R.string.error_uploading_s3_part2) + ex.getMessage());
hideProgressDialog(TAG);
showDialogMessage(getString(error), getString(R.string.error_failed_create_image_alert_id) + error);

}

});

}
public void addImageToGallery(final String filePath) {
ContentValues values = new ContentValues();

values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.MediaColumns.DATA, filePath);

getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}

希望有帮助:)。我会警告您,但当您启动 setDrawCacheEnabled 时,它可能会在屏幕上出现奇怪的行为,并导致您需要重绘,因为它有时似乎会消除约束。因此,我通常只是打开一个新的空白屏幕,其中包含要转换为图像的内容,完成工作然后关闭。

因此,只需看看它对您有何作用,然后决定从哪里开始。我尝试过的大多数库也有同样的问题,这就是为什么我只编写自己的库。

关于java - 如何以编程方式更改图像的不透明度并将其保存在图库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46348985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com