簡體   English   中英

以人像捕獲圖像,但在目錄中獲得橫向輸出

[英]Capture image in portrait but get an ouput of landscape in directory

我正在用android制作自己的相機,然后每當我捕獲圖像時,圖像方向就可以了。 但是當我在外部sdcard文件中打開它時,它就像這樣: 在此處輸入圖片說明

這是我的代碼:

public class CameraApp extends Activity implements SurfaceHolder.Callback {
private static final String TAG = "IMG_";
private static final String IMAGE_FOLDER = "/PhoneController/";
private static final String EXTENTION = ".jpg";
private String pictureName = "";
public String picNameToshare = "";
static final int FOTO_MODE = 0;
String speed;
String imageFilePath;
private Handler mHandler = new Handler();
public static String imageFilePath1;
FileOutputStream fos = null;
public Bitmap framebmpScaled;
public Bitmap SelecedFrmae;
public static Bitmap mergeBitmap;
public static Bitmap bmpfULL;
Camera camera;
Button button;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
LayoutInflater controlInflater = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.camera);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    getWindow().setFormat(PixelFormat.UNKNOWN);
    surfaceView = (SurfaceView) findViewById(R.id.camerapreview);
    surfaceHolder = surfaceView.getHolder();

    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(buttonListener);
}

private OnClickListener buttonListener = new OnClickListener() {

    public void onClick(View v) {
        Handler myHandler = new Handler();
        if (previewing) {
            camera.stopPreview();
            previewing = false;
            Log.e("", "one");
        }

        if (camera != null) {
            try {
                camera.setPreviewDisplay(surfaceHolder);
                camera.startPreview();
                previewing = true;
                myHandler.postDelayed(mMyRunnable, 2000); // called after 5
                                                            // seconds
                button.setText("Waiting...");
                Log.e("", "two");

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        /*
         * Handler myHandler = new Handler();
         * myHandler.postDelayed(mMyRunnable, 5000); // called after 5
         * seconds button.setText("Waiting...");
         */
    }
};

private Runnable mMyRunnable = new Runnable() {
    public void run() {
        Camera.Parameters params = camera.getParameters();
        params.set("rotation", 90);
        camera.setParameters(params);
        camera.takePicture(myShutterCallback, myPictureCallback_RAW,
                myPictureCallback_JPG);
        storePicture(mergeBitmap);
        button.setText("Capture");
    }
};

PictureCallback myPictureCallback_JPG = new PictureCallback() {
    public void onPictureTaken(byte[] arg0, Camera arg1) {
        // TODO Auto-generated method stub
        Display display = getWindowManager().getDefaultDisplay();
        final int ScreenWidth = display.getWidth();
        final int ScreenHeight = display.getHeight();
        Log.e("" + display.getWidth(), "" + display.getWidth());

        Bitmap bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0,
                arg0.length);
        Bitmap bmpScaled1 = Bitmap.createScaledBitmap(bitmapPicture,
                ScreenWidth, ScreenHeight, true);
        mergeBitmap = bmpScaled1;
        showPicture();
    }
};

public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub
    if (previewing) {
        camera.stopPreview();
        previewing = false;
    }

    if (camera != null) {
        try {
            camera.setPreviewDisplay(surfaceHolder);
            camera.startPreview();
            previewing = true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    camera = Camera.open();
    camera.setDisplayOrientation(90);
}

public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    camera.stopPreview();
    camera.release();
    camera = null;
    previewing = false;
}

void storePicture(Bitmap bm) {

    mHandler.post(new Runnable() {
        public void run() {
            // showPicture();
        }
    });
    String timeStamp = new SimpleDateFormat("yyyMMdd_HHmmss")
            .format(new Date());
    this.pictureName = TAG + timeStamp;
    picNameToshare = this.pictureName;
    this.imageFilePath = IMAGE_FOLDER + this.pictureName + EXTENTION;
    this.imageFilePath = sanitizePath(this.imageFilePath);

    try {
        checkSDCard(this.imageFilePath);

        fos = new FileOutputStream(this.imageFilePath);

        if (fos != null) {
            bm.compress(Bitmap.CompressFormat.JPEG, 85, fos);
            Toast.makeText(this, "Image Saved", Toast.LENGTH_SHORT).show();

            fos.close();

        }
    } catch (IOException ioe) {

        Log.e(TAG, "CapturePicture : " + ioe.toString());
    } catch (Exception e) {

        Log.e(TAG, "CapturePicture : " + e.toString());
    }
    sendBroadcast(new Intent(
            Intent.ACTION_MEDIA_MOUNTED,
            Uri.parse("file://" + Environment.getExternalStorageDirectory())));
    imageFilePath1 = this.imageFilePath;
}

/**
 * Check the SDCard is mounted on device
 * 
 * @param path
 *            of image file
 * @throws IOException
 */
void checkSDCard(String path) throws IOException {
    String state = android.os.Environment.getExternalStorageState();
    if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
        Toast.makeText(this,
                "Please insert sdcard other wise image won't stored",
                Toast.LENGTH_SHORT).show();
        throw new IOException("SD Card is not mounted.  It is " + state
                + ".");
    }

    // make sure the directory we plan to store the recording is exists
    File directory = new File(path).getParentFile();
    if (!directory.exists() && !directory.mkdirs()) {
        throw new IOException("Path to file could not be created.");
    }
}

private String sanitizePath(String path) {
    if (!path.startsWith("/")) {
        path = "/" + path;
    }

    return Environment.getExternalStorageDirectory().getAbsolutePath()
            + path;
}

請幫我。

試試這個

Camera.Parameters parameters = camera.getParameters();
parameters.set("orientation", "portrait");
camera.setParameters(parameters);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM