|
|
|
@@ -18,44 +18,54 @@ package com.mindspore.styletransfer; |
|
|
|
import android.content.Intent; |
|
|
|
import android.content.res.Configuration; |
|
|
|
import android.graphics.Bitmap; |
|
|
|
import android.graphics.BitmapFactory; |
|
|
|
import android.net.Uri; |
|
|
|
import android.os.Bundle; |
|
|
|
import android.os.Environment; |
|
|
|
import android.provider.MediaStore; |
|
|
|
import android.text.TextUtils; |
|
|
|
import android.util.Log; |
|
|
|
import android.util.Pair; |
|
|
|
import android.view.View; |
|
|
|
import android.widget.Button; |
|
|
|
import android.widget.ImageView; |
|
|
|
import android.widget.ProgressBar; |
|
|
|
import android.widget.TextView; |
|
|
|
import android.widget.Toast; |
|
|
|
|
|
|
|
import androidx.annotation.Nullable; |
|
|
|
import androidx.appcompat.app.AppCompatActivity; |
|
|
|
import androidx.core.content.FileProvider; |
|
|
|
import androidx.recyclerview.widget.GridLayoutManager; |
|
|
|
import androidx.recyclerview.widget.RecyclerView; |
|
|
|
|
|
|
|
import com.alibaba.android.arouter.facade.annotation.Route; |
|
|
|
import com.bumptech.glide.Glide; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.List; |
|
|
|
import java.io.File; |
|
|
|
import java.io.FileNotFoundException; |
|
|
|
|
|
|
|
@Route(path = "/styletransfer/StyleMainActivity") |
|
|
|
public class StyleMainActivity extends AppCompatActivity implements View.OnClickListener, OnListFragmentInteractionListener { |
|
|
|
public class StyleMainActivity extends AppCompatActivity implements OnBackgroundImageListener { |
|
|
|
|
|
|
|
private static final String TAG = "StyleMainActivity"; |
|
|
|
|
|
|
|
private static final int[] IMAGES = {R.drawable.style0, R.drawable.style1, R.drawable.style2, R.drawable.style3, R.drawable.style4, |
|
|
|
R.drawable.style5, R.drawable.style6, R.drawable.style7, R.drawable.style8, R.drawable.style9, |
|
|
|
R.drawable.style10, R.drawable.style11, R.drawable.style12, R.drawable.style13, R.drawable.style14, |
|
|
|
R.drawable.style15, R.drawable.style16, R.drawable.style17, R.drawable.style18, R.drawable.style19, R.drawable.add}; |
|
|
|
|
|
|
|
private static final int RC_CHOOSE_PHOTO = 1; |
|
|
|
private static final int RC_CHOOSE_PHOTO_FOR_BACKGROUND = 11; |
|
|
|
private static final int RC_CHOOSE_CAMERA = 2; |
|
|
|
|
|
|
|
|
|
|
|
private StyleTransferModelExecutor transferModelExecutor; |
|
|
|
|
|
|
|
private boolean isRunningModel; |
|
|
|
|
|
|
|
private ImageView imgOrigin; |
|
|
|
private Button btnImage; |
|
|
|
private ImageView imgPreview; |
|
|
|
private Uri imageUri; |
|
|
|
private TextView textOriginImage; |
|
|
|
private ProgressBar progressBar; |
|
|
|
|
|
|
|
private RecyclerView recyclerView; |
|
|
|
|
|
|
|
@@ -63,8 +73,7 @@ public class StyleMainActivity extends AppCompatActivity implements View.OnClick |
|
|
|
private Integer maxHeightOfImage; |
|
|
|
private boolean isLandScape; |
|
|
|
|
|
|
|
private Bitmap originBitmap, styleBitmap; |
|
|
|
private String selectedStyle; |
|
|
|
private Bitmap originBitmap, styleBitmap, resultBitmap; |
|
|
|
|
|
|
|
@Override |
|
|
|
protected void onCreate(Bundle savedInstanceState) { |
|
|
|
@@ -75,52 +84,78 @@ public class StyleMainActivity extends AppCompatActivity implements View.OnClick |
|
|
|
} |
|
|
|
|
|
|
|
private void init() { |
|
|
|
imgOrigin = findViewById(R.id.img_origin); |
|
|
|
btnImage = findViewById(R.id.btn_image); |
|
|
|
imgOrigin.setOnClickListener(this); |
|
|
|
btnImage.setOnClickListener(this); |
|
|
|
|
|
|
|
imgPreview = findViewById(R.id.img_origin); |
|
|
|
textOriginImage = findViewById(R.id.tv_image); |
|
|
|
progressBar = findViewById(R.id.progress); |
|
|
|
recyclerView = findViewById(R.id.recyclerview); |
|
|
|
List<String> styles = new ArrayList<>(); |
|
|
|
try { |
|
|
|
styles.addAll(Arrays.asList(getAssets().list("thumbnails"))); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
|
|
|
|
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3); |
|
|
|
recyclerView.setLayoutManager(gridLayoutManager); |
|
|
|
recyclerView.setAdapter(new StyleRecyclerViewAdapter(this, styles, this)); |
|
|
|
|
|
|
|
recyclerView.setLayoutManager(new GridLayoutManager(this, 3)); |
|
|
|
recyclerView.setAdapter(new StyleRecyclerViewAdapter(this, IMAGES, this)); |
|
|
|
transferModelExecutor = new StyleTransferModelExecutor(this, false); |
|
|
|
} |
|
|
|
|
|
|
|
public void onClickPhoto(View view) { |
|
|
|
openGallay(RC_CHOOSE_PHOTO); |
|
|
|
textOriginImage.setVisibility(View.GONE); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onClick(View view) { |
|
|
|
if (view.getId() == R.id.img_origin || view.getId() == R.id.btn_image) { |
|
|
|
btnImage.setVisibility(View.GONE); |
|
|
|
openGallay(); |
|
|
|
public void onClickCamera(View view) { |
|
|
|
openCamera(); |
|
|
|
textOriginImage.setVisibility(View.GONE); |
|
|
|
} |
|
|
|
|
|
|
|
public void onClickRecovery(View view) { |
|
|
|
if (originBitmap != null) { |
|
|
|
Glide.with(this).load(originBitmap).into(imgPreview); |
|
|
|
} else { |
|
|
|
Toast.makeText(this, "Please select an original picture first", Toast.LENGTH_SHORT).show(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void onClickSave(View view) { |
|
|
|
if (this.resultBitmap == null) { |
|
|
|
Log.e(TAG, "null processed image"); |
|
|
|
Toast.makeText(this.getApplicationContext(), R.string.no_pic_neededSave, Toast.LENGTH_SHORT).show(); |
|
|
|
} else { |
|
|
|
ImageUtils.saveToAlbum(getApplicationContext(), this.resultBitmap); |
|
|
|
Toast.makeText(this.getApplicationContext(), R.string.save_success, Toast.LENGTH_SHORT).show(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void openGallay() { |
|
|
|
private void openGallay(int request) { |
|
|
|
Intent intentToPickPic = new Intent(Intent.ACTION_PICK, null); |
|
|
|
intentToPickPic.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); |
|
|
|
startActivityForResult(intentToPickPic, RC_CHOOSE_PHOTO); |
|
|
|
startActivityForResult(intentToPickPic, request); |
|
|
|
} |
|
|
|
|
|
|
|
private void openCamera() { |
|
|
|
Intent intentToTakePhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
|
|
|
String mTempPhotoPath = Environment.getExternalStorageDirectory() + File.separator + "photo.jpeg"; |
|
|
|
imageUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".fileprovider", new File(mTempPhotoPath)); |
|
|
|
intentToTakePhoto.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); |
|
|
|
startActivityForResult(intentToTakePhoto, RC_CHOOSE_CAMERA); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { |
|
|
|
super.onActivityResult(requestCode, resultCode, data); |
|
|
|
|
|
|
|
if (RC_CHOOSE_PHOTO == requestCode && null != data && null != data.getData()) { |
|
|
|
if (data != null) { |
|
|
|
this.imageUri = data.getData(); |
|
|
|
showOriginImage(); |
|
|
|
if (resultCode == RESULT_OK) { |
|
|
|
if (RC_CHOOSE_PHOTO == requestCode) { |
|
|
|
if (null != data && null != data.getData()) { |
|
|
|
this.imageUri = data.getData(); |
|
|
|
showOriginImage(); |
|
|
|
} else { |
|
|
|
finish(); |
|
|
|
} |
|
|
|
} else if (RC_CHOOSE_PHOTO_FOR_BACKGROUND == requestCode) { |
|
|
|
if (null != data && null != data.getData()) { |
|
|
|
showCustomBack(data.getData()); |
|
|
|
} else { |
|
|
|
finish(); |
|
|
|
} |
|
|
|
} else if (RC_CHOOSE_CAMERA == requestCode) { |
|
|
|
showOriginCamera(); |
|
|
|
} |
|
|
|
} else { |
|
|
|
finish(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@@ -128,49 +163,81 @@ public class StyleMainActivity extends AppCompatActivity implements View.OnClick |
|
|
|
Pair<Integer, Integer> targetedSize = this.getTargetSize(); |
|
|
|
int targetWidth = targetedSize.first; |
|
|
|
int maxHeight = targetedSize.second; |
|
|
|
originBitmap = BitmapUtils.loadFromPath(StyleMainActivity.this, imageUri, targetWidth, maxHeight); |
|
|
|
originBitmap = BitmapUtils.loadFromPath(this, imageUri, targetWidth, maxHeight); |
|
|
|
// Determine how much to scale down the image. |
|
|
|
Log.i(TAG, "resized image size width:" + originBitmap.getWidth() + ",height: " + originBitmap.getHeight()); |
|
|
|
Log.e(TAG, "resized image size width:" + originBitmap.getWidth() + ",height: " + originBitmap.getHeight()); |
|
|
|
if (originBitmap != null) { |
|
|
|
Glide.with(this).load(originBitmap).into(imgPreview); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void showOriginCamera() { |
|
|
|
try { |
|
|
|
Pair<Integer, Integer> targetedSize = this.getTargetSize(); |
|
|
|
int targetWidth = targetedSize.first; |
|
|
|
int maxHeight = targetedSize.second; |
|
|
|
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri)); |
|
|
|
originBitmap = BitmapUtils.zoomImage(bitmap, targetWidth, maxHeight); |
|
|
|
} catch (FileNotFoundException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
// Determine how much to scale down the image. |
|
|
|
Log.e(TAG, "resized image size width:" + originBitmap.getWidth() + ",height: " + originBitmap.getHeight()); |
|
|
|
if (originBitmap != null) { |
|
|
|
Glide.with(this).load(originBitmap).into(imgOrigin); |
|
|
|
Glide.with(this).load(originBitmap).into(imgPreview); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void showCustomBack(Uri imageUri) { |
|
|
|
Pair<Integer, Integer> targetedSize = this.getTargetSize(); |
|
|
|
int targetWidth = targetedSize.first; |
|
|
|
int maxHeight = targetedSize.second; |
|
|
|
styleBitmap = BitmapUtils.loadFromPath(this, imageUri, targetWidth, maxHeight); |
|
|
|
startRunningModel(styleBitmap); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onListFragmentInteraction(String item) { |
|
|
|
this.selectedStyle = item; |
|
|
|
startRunningModel(); |
|
|
|
public void onBackImageSelected(int position) { |
|
|
|
styleBitmap = BitmapFactory.decodeResource(getResources(), IMAGES[position]); |
|
|
|
startRunningModel(styleBitmap); |
|
|
|
} |
|
|
|
|
|
|
|
private void startRunningModel() { |
|
|
|
if (!isRunningModel && !TextUtils.isEmpty(selectedStyle)) { |
|
|
|
styleBitmap = ImageUtils.loadBitmapFromResources(this, getUriFromAssetThumb(selectedStyle)); |
|
|
|
if (originBitmap == null) { |
|
|
|
Toast.makeText(this, "Please select an original picture first", Toast.LENGTH_SHORT).show(); |
|
|
|
return; |
|
|
|
} |
|
|
|
@Override |
|
|
|
public void onImageAdd(View view) { |
|
|
|
openGallay(RC_CHOOSE_PHOTO_FOR_BACKGROUND); |
|
|
|
} |
|
|
|
|
|
|
|
private void startRunningModel(Bitmap styleBitmap) { |
|
|
|
if (originBitmap == null) { |
|
|
|
Toast.makeText(this, "Please select an original picture first", Toast.LENGTH_SHORT).show(); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
if (!isRunningModel) { |
|
|
|
isRunningModel = true; |
|
|
|
progressBar.setVisibility(View.VISIBLE); |
|
|
|
ModelExecutionResult result = transferModelExecutor.execute(originBitmap, styleBitmap); |
|
|
|
Glide.with(this).load(result.getStyledImage()).into(imgOrigin); |
|
|
|
if (null != result && null != result.getStyledImage()) { |
|
|
|
resultBitmap = BitmapUtils.changeBitmapSize(result.getStyledImage(), originBitmap.getWidth(), originBitmap.getHeight()); |
|
|
|
Log.e("AAA", "w>>" + resultBitmap.getWidth() + ">>>h>>" + resultBitmap.getHeight()); |
|
|
|
Glide.with(this).load(resultBitmap).override(resultBitmap.getWidth(), resultBitmap.getHeight()).into(imgPreview); |
|
|
|
} else { |
|
|
|
Toast.makeText(this, "ModelExecute failed", Toast.LENGTH_SHORT).show(); |
|
|
|
} |
|
|
|
isRunningModel = false; |
|
|
|
progressBar.setVisibility(View.INVISIBLE); |
|
|
|
} else { |
|
|
|
Toast.makeText(this, "Previous Model still running", Toast.LENGTH_SHORT).show(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private String getUriFromAssetThumb(String thumb) { |
|
|
|
return "thumbnails/" + thumb; |
|
|
|
} |
|
|
|
|
|
|
|
// Returns max width of image. |
|
|
|
private Integer getMaxWidthOfImage() { |
|
|
|
if (this.maxWidthOfImage == null) { |
|
|
|
if (this.isLandScape) { |
|
|
|
this.maxWidthOfImage = ((View) this.imgOrigin.getParent()).getHeight(); |
|
|
|
this.maxWidthOfImage = ((View) this.imgPreview.getParent()).getHeight(); |
|
|
|
} else { |
|
|
|
this.maxWidthOfImage = ((View) this.imgOrigin.getParent()).getWidth(); |
|
|
|
this.maxWidthOfImage = ((View) this.imgPreview.getParent()).getWidth(); |
|
|
|
} |
|
|
|
} |
|
|
|
return this.maxWidthOfImage; |
|
|
|
@@ -180,9 +247,9 @@ public class StyleMainActivity extends AppCompatActivity implements View.OnClick |
|
|
|
private Integer getMaxHeightOfImage() { |
|
|
|
if (this.maxHeightOfImage == null) { |
|
|
|
if (this.isLandScape) { |
|
|
|
this.maxHeightOfImage = ((View) this.imgOrigin.getParent()).getWidth(); |
|
|
|
this.maxHeightOfImage = ((View) this.imgPreview.getParent()).getWidth(); |
|
|
|
} else { |
|
|
|
this.maxHeightOfImage = ((View) this.imgOrigin.getParent()).getHeight(); |
|
|
|
this.maxHeightOfImage = ((View) this.imgPreview.getParent()).getHeight(); |
|
|
|
} |
|
|
|
} |
|
|
|
return this.maxHeightOfImage; |
|
|
|
|