我需要你的帮助,如果你很友好,我需要解决我在Android应用中遇到的问题:
我设法在MainActivity中捕获图片并将其显示在一个单独的活动中 - PictureActivity。我的代码如下:
在 MainActivity 中,我有
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
/**
* This is called in tap on a graphic element in my MainActivity layout
*/
public void launchCamera(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
Bitmap imageData = null;
if (resultCode == RESULT_OK) {
imageData = (Bitmap) data.getExtras().get("data");
Intent i = new Intent(this, PictureActivity.class);
i.putExtra("captured_picture", imageData);
startActivity(i);
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
Toast.makeText(getApplicationContext(), R.string.picture_capture_error, Toast.LENGTH_SHORT).show();
}
}
}
我的 PictureActivity 如下所示:
public class PictureActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_picture);
Bitmap bitmap = getIntent().getExtras().getParcelable("captured_picture");
ImageView view = (ImageView) findViewById(R.id.preview_photo);
view.setImageBitmap(bitmap);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.image_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
}
我的 PictureActivity布局看起来像这样
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/preview_photo"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
这是迄今为止最终结果的表现:
长话短说,这些是我想要做的事情下一步:
接下来,将图像保存在某处(在共享首选项或会话中可能?),然后将其上传到远程服务器。我说“将图像保存在某个地方”因为用户可以选择拍摄第二张照片(最多两张)并执行相同的动作,如上所述(通过点击第一个按钮)从右边开始,在主动作栏中。)
到目前为止我无法弄清楚:
很抱歉这篇长篇文章,并提前感谢您的帮助!
答案 0 :(得分:0)
回答你的3个问题:
以下是一些可能对您有用的代码:
public static void saveBitmapToFile(Bitmap bitmap, String filename){
FileOutputStream out = null;
try {
out = new FileOutputStream(filename);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
// PNG is a lossless format, the compression factor (100) is ignored
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static Bitmap cropAndRotateBitmap(Bitmap bitmap, Rect cropRect, int rotateByDegrees) {
Log.v(TAG, "cropAndRotateBitmap()");
Log.v(TAG, "bitmap: " + bitmap);
//Log.v(TAG, "orientation: " + orientation);
Log.v(TAG, "degrees: " + rotateByDegrees);
Log.v(TAG, "cropRect: " + cropRect);
int cropRectWidth = cropRect.right - cropRect.left;
int cropRectHeight = cropRect.bottom - cropRect.top;
System.gc();
Bitmap result;
Matrix m = new Matrix();
Canvas canvas = new Canvas();
if (rotateByDegrees == 0) {
result = Bitmap.createBitmap(cropRectWidth, cropRectHeight, Bitmap.Config.ARGB_8888);
canvas = new Canvas(result);
m.postTranslate(-cropRect.left, -cropRect.top);
} else if (rotateByDegrees == 90) {
Log.v(TAG, "rotate 90, cropRect: " + cropRect);
result = Bitmap.createBitmap(cropRectHeight, cropRectWidth, Bitmap.Config.ARGB_8888);
canvas = new Canvas(result);
m.postTranslate(-cropRect.left, -cropRect.height() - cropRect.top);
m.postRotate(90);
} else if (rotateByDegrees == 180) {
Log.v(TAG, "rotate 180");
result = Bitmap.createBitmap(cropRectWidth, cropRectHeight, Bitmap.Config.ARGB_8888);
canvas = new Canvas(result);
m.postTranslate(-cropRect.left - cropRect.width(), -cropRect.height() - cropRect.top);
m.postRotate(180);
} else { // 270
Log.v(TAG, "rotate 270");
result = Bitmap.createBitmap(cropRectHeight, cropRectWidth, Bitmap.Config.ARGB_8888);
canvas = new Canvas(result);
m.postTranslate(-cropRect.width() - cropRect.left, -cropRect.top);
m.postRotate(270);
}
Paint whitePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
whitePaint.setStyle(Paint.Style.FILL);
whitePaint.setColor(Color.WHITE);
canvas.drawBitmap(bitmap, m, whitePaint);
// canvas.restore();
return result;
}
public static Bitmap rotateBitmap(Bitmap bitmap, int rotateByDegrees) {
Log.v(TAG, "rotateBitmap()");
Log.v(TAG, "degrees: " + rotateByDegrees);
System.gc();
Bitmap result;
Canvas canvas = new Canvas();
if (rotateByDegrees == 0) {
result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
canvas = new Canvas(result);
canvas.save();
} else if (rotateByDegrees == 90) {
result = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getWidth(), Bitmap.Config.ARGB_8888);
canvas = new Canvas(result);
canvas.save();
canvas.rotate(90);
canvas.translate(0, -1 * bitmap.getHeight());
} else if (rotateByDegrees == 180) {
result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
canvas = new Canvas(result);
canvas.save();
canvas.rotate(180);
canvas.translate(-1 * bitmap.getWidth(), -1 * bitmap.getHeight());
} else { // 270
result = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getWidth(), Bitmap.Config.ARGB_8888);
canvas = new Canvas(result);
canvas.save();
canvas.rotate(270);
canvas.translate(-1 * bitmap.getWidth(), 0);
}
canvas.drawBitmap(bitmap, new Matrix(), null);
canvas.restore();
return result;
}